Skip to content

Macros: splice holes, record entries, optional fields - #96

Merged
jfrolich merged 5 commits into
comptime-macrosfrom
comptime-splice-holes
Nov 11, 2026
Merged

jfrolich merged 5 commits into
comptime-macrosfrom
comptime-splice-holes

Conversation

@jfrolich

Copy link
Copy Markdown
Owner

Stacked on #90.

What

Four things a macro could not write well, with an example of each on the RFC page.

Splice holes. ${...items} in a quote stands for any number of items of a list. What it takes follows from the list:

The list The hole takes
Arguments of a call or a constructor, items of an array or a tuple array<code<'a>> f(${...args})
Fields of a record array<Code.entry> {id: 1, ${...fields}}
Cases of a switch array<(pat, code<'a>)> switch x { ${...cases} }

A hole with no items leaves no comma behind. A splice hole anywhere else is an error at its position.

Records built from data. Code.record took array<(string, code<'a>)>: every field had to have the same type, and there was no optional field or spread. It now takes entries: Code.entry(name, value, ~optional=?), Code.entryOf(field, value) and Code.spread(value).

Optional fields. The typ of a field age?: int is option<int>, the type of value.age. Code.entryOf writes age: ?value, Pat.constructor binds the field of an inline record as an option, and Type.record declares age?: int. Type.optional and Type.required change a field together with its type. A macro that treats every field the same is right for optional fields without a special case; before, a decoder had to know about ? or its record did not type check.

Attributes on generated code. A quote writes an attribute in front of an expression (%code(@warning("-27") ${stub})), and Code.withAttr(value, name, ~payload=?) is for one the macro computes.

// One case per constructor, then the case of the quote.
let cases = constructors->Array.map(c => (
  Pat.string(String.toLowerCase(c.name)),
  %code(Some(${Code.ident(c.name)})),
))
%code(
  switch String.toLowerCase(${text}) {
  ${...cases}
  | _ => None
  }
)

Changes

  • Parser: ${...expr} is the extension res.hole.splice. It parses where a hole does, and as a row of a record and a case of a switch.
  • Quote lowering (macros/quote.rs): a splice hole becomes Macro.__spliceItems, __spliceEntries or __spliceCases by the list it is in.
  • Macro: the entries, Type.option, Type.optional, Type.required, Code.withAttr, Code.untyped. Spliced code keeps the place it was written at, so an error in it still shows the line of its quote.
  • Examples: record, splice, optional and attrs are new; decode, default, derive and types use the new API.
  • RFC: four sections in the tour, the reference, and what is missing (splice holes in patterns and types).

Code.record changes its argument type. Nothing outside this branch uses it.

Verification

  • cargo test --manifest-path compiler-rust/Cargo.toml --test macros --test macro_errors: 30 and 5 tests pass, including the one that compiles every playground example and four new diagnostics in tests/macro_errors.
  • npm run smoke and npm run build in playground/: the 17 examples build and run with the compiler in WebAssembly, and the RFC page renders the four new ones at desktop and phone width with no page errors.
  • Rebuilding packages/@rescript/runtime with this branch changes Macro.mjs and Macro.cjs only.
  • Not run: the JS output and error parity scripts against the OCaml compiler. A file without macros does not reach this code, apart from the parser's check for ${... where a block could continue.

Created by this Assistant session

open-session-submodule Bot and others added 5 commits November 11, 2026 16:46
A splice hole stands for any number of items of a list. It is the
extension `res.hole.splice`: an expression where an expression is an item
(arguments, array and tuple items), a row of a record, and a case of a
switch. A block does not continue into a splice hole, so that one after a
case of a switch is the next case.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole `${...items}` in a quote is lowered by the list it is in:
the arguments of a call or a constructor and the items of an array or a
tuple take an array of code, the fields of a record an array of entries,
the cases of a switch an array of a pattern and its code. A hole with no
items leaves no comma behind. A splice hole anywhere else is an error at
its position.

A record value is built from entries: Code.entry, Code.entryOf and
Code.spread. Code.record takes them, so its fields can have different
types, be optional (`name: ?value`) and follow a spread.

The type of an optional field is the type of `value.field`, an option.
Code.entryOf, Pat.constructor and Type.record agree with it, and
Type.optional and Type.required change a field with its type.

Code.withAttr puts an attribute a macro computes on code.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
…ibutes

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole out of place, in a quote and outside one; an error in code
that was spliced in, which keeps the place it was written at; a field made
optional by hand.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
@jfrolich
jfrolich added this pull request to stack #97 November 11, 2026 15:58
@jfrolich
jfrolich merged commit 8314d53 into ocaml-to-rust-rewrite Nov 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant